package com.ht.medicaltreatment.fragment

import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.ActivityUtils
import com.gym.baselib.base.BaseActivity
import com.gym.baselib.base.BaseFragment
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.http.api.SaveSampleBoxAPI
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.view.RefrigeratorLocationDialog
import com.ht.medicaltreatment.view.SampleFormalDialog
import com.ht.medicaltreatment.view.ShowModelDialog
import com.ht.medicaltreatment.viewmodel.SampleBoxViewModel
import kotlinx.android.synthetic.main.activity_build_single_sample_box.*

//创建样本盒 - 单独创建
class SingleCreateSampleBoxFragment:BaseFragment(), View.OnClickListener {
    override fun getLayoutResId() = R.layout.activity_build_single_sample_box

    private lateinit var mModel: SampleBoxViewModel
    //行
    private var sampleBoxStructureRow = ""
    //列
    private var sampleBoxStructureCol = ""
    //样本盒位置id
    private var sampleBoxStruId  = ""
    //UID
    private var rfidCode   = ""
    //样本盒名称
    private var name   = ""
    //显示模式
    private var sampleBoxStructureSpec   = ""

    override fun init() {
        mModel = ViewModelProvider(this)[SampleBoxViewModel::class.java]
        llLocation.setOnClickListener(this)
        llFormat.setOnClickListener(this)
        llUID.setOnClickListener(this)
        tvCreate.setOnClickListener(this)
        llModel.setOnClickListener(this)

        ScanUtils.init()
    }

    override fun onClick(v: View?) {
        when(v){
            //位置
            llLocation -> RefrigeratorLocationDialog(activity as BaseActivity).show { name, id ->
                tvLocation.text = name
                sampleBoxStruId = id
            }
            //规格
            llFormat -> SampleFormalDialog(requireContext()).show {H,L ->
                sampleBoxStructureRow = H
                sampleBoxStructureCol = L
                tvProject.text = "$H*$L"
            }
            //UID
            llUID ->ScanUtils.open {
                rfidCode = it
                tvRFID.text = it
            }
            //显示模式
            llModel -> ShowModelDialog(requireContext()).show {name,id ->
                sampleBoxStructureSpec = "$id"
                tvFrom.text = name
            }
            //创建
            tvCreate ->{
                name = etName.text.toString()
                if(name.isEmpty()||
                    sampleBoxStructureRow.isEmpty()||
                    sampleBoxStructureCol.isEmpty()||
                    rfidCode.isEmpty()||
                    sampleBoxStructureSpec.isEmpty()
                ){
                    showToast("请补全信息")
                    return
                }
                showProgress()
                val bean = SaveSampleBoxAPI(
                    name = name,
                    rfidCode = rfidCode,
                    sampleBoxStruId = sampleBoxStruId,
                    sampleBoxStructureCol = sampleBoxStructureCol,
                    sampleBoxStructureRow = sampleBoxStructureRow,
                    sampleBoxStructureSpec = sampleBoxStructureSpec
                )
                mModel.createSampleBox(bean,{
                    dismissProgress()
                    showToast("创建成功")
                    activity?.finish()
                })
            }
        }
    }
}
